﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Entity;

namespace Web.admin.InnerManage.Participants
{
    public partial class addStudent : System.Web.UI.Page
    {
        StudentService studentService = new StudentService();
        ParticipantService participantService = new ParticipantService();
        GroupsService groupsService = new GroupsService();
        ContestService contestService = new ContestService();
        ClassService classService=new ClassService();
        AcademyService academyService=new AcademyService();
        CompetitionService competitionService = new CompetitionService();

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {

                string contestId = Request["ContestId"];
                if (!String.IsNullOrEmpty(contestId))
                {
                    ConId.Value = contestId;

                    BoContests contestmodel = contestService.getContest(Request.QueryString["ContestId"]);
                    ContestsList.Items.Add(new ListItem(contestmodel.CON_NAME, contestmodel.CON_ID));

                    GroupsList.DataSource = groupsService.getContestGroups(contestId);
                    GroupsList.DataTextField = "GRO_NAME";
                    GroupsList.DataValueField = "GRO_ID";
                    GroupsList.DataBind();
                }

                string operation = Request["OPERATION"];
                if (operation == null)
                {
                    return;
                }
                string compId = Request["id"];
                //if (compId == null)
                //    return;
                if (operation == "Modify")
                {
                    ModifyInner(compId);
                }
                if (operation == "AddGroStudent")
                {
                    if (!string.IsNullOrEmpty(Request.QueryString["GroupId"]))
                    { 
                        AddGroStudent(Request.QueryString["GroupId"]);
                    }
                }

                else if (operation == "Delete")
                {
                    DeleteInner(compId);
                }
            }
        }

        /// <summary>
        /// 修改竞赛
        /// </summary>
        /// <param name="parId"></param>
        protected void ModifyInner(string parId) 
        {
            //BoCompetitions comp = competitionService.getCompetition(compId);
            BoParticipants par=new BoParticipants();
            par= participantService.getParticipant(parId);
            ParId.Value = parId;
            stuCode_txt.Value = par.STU_CODE;

            ContestsList.Items.Clear();
            BoContests contestmodel = contestService.getContest(Request.QueryString["ContestId"]);
            ContestsList.Items.Add( new ListItem(contestmodel.CON_NAME,contestmodel.CON_ID));

            GroupsList.DataSource = groupsService.getContestGroups(par.PAR_CONTESTS);
            GroupsList.DataTextField = "GRO_NAME";
            GroupsList.DataValueField = "PAR_GROUPS";
            GroupsList.DataBind();
            GroupsList.SelectedValue = par.PAR_GROUPS;

            foreach (ListItem item in IsSpecialtyList.Items)
            {
                if (par.PAR_SPECIALTY.ToString() == item.Value)
                    item.Selected = true;
                else
                    item.Selected = false;
            }
        }

        /// <summary>
        /// 添加小组学生
        /// </summary>
        /// <param name="GroupId">小组id</param>
        protected void AddGroStudent(string GroupId)
        {
            //对小组进行绑定
            BoGroups GroupModel = groupsService.getGroups(GroupId);
            GroupsList.Items.Clear();//清空小组下拉列表
            GroupsList.Items.Add(new ListItem(GroupModel.GRO_NAME,GroupModel.GRO_ID));//绑定小组

            //对竞赛进行绑定
            ContestsList.Items.Clear();//清空竞赛下拉列表
            BoContests contestModel = contestService.getContest(GroupModel.GRO_CONTESTS);
            ContestsList.Items.Add(new ListItem(contestModel.CON_NAME,contestModel.CON_ID));//绑定竞赛

            //对是否专业组进行绑定
            IsSpecialtyList.SelectedValue = GroupModel.GRO_SPECIALTY==1?"1":"0";
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        protected void Bind_Data(string Con_id)
        { 
            
        }
        protected void DeleteInner(string parId) 
        {
            BoParticipants par = participantService.getParticipant(parId);
            participantService.RemoveParticipant(par);
            Response.Redirect("../InnerPar.aspx");
            
        }
        protected void btn_Save_Click(object sender, EventArgs e) 
        {
            BoParticipants par = new BoParticipants();
            BoStudents student = studentService.getStudentByCode(stuCode_txt.Value);
            par.PAR_STUDENTS = student.STU_ID;
            par.PAR_GROUPS = GroupsList.SelectedValue;
            par.PAR_SPECIALTY = IsSpecialtyList.SelectedValue == "1" ? 1 : 0;
            par.PAR_CONTESTS = ContestsList.SelectedValue;
            if (participantService.CheckStuId(par) > 0)
            {
                Response.Write("<script>alert('该学生已经报名，不允许重复报名')</script>");
            }
            else
            {
                if (string.IsNullOrEmpty(ParId.Value) )
                {
                    BoStudents studnetmodel = studentService.GetV_studentByCode(stuCode_txt.Value);
                    if (studnetmodel != null)
                    {
                        participantService.CreateParticipant(par);
                    }
                    else
                    {
                        Response.Write("<script>alert('未检测该学生，请确定学号正确')</script>");
                        return;
                    }
                }
                else
                {
                    par.PAR_ID = ParId.Value;
                    participantService.UpdateParticipant(par);
                }
                if (Request["OPERATION"] == "AddGroStudent")
                {
                    Response.Redirect("GroupMember.aspx?GroupId=" + GroupsList.SelectedValue );
                }

                Response.Redirect("../InnerPar.aspx?id=" + ContestsList.SelectedValue);
            }
        }
        protected void label_change(object sender, EventArgs e) 
        {
            //BoParticipants par = participantService.getParticipant(ParId.Value);
            //System.Text.StringBuilder st = new System.Text.StringBuilder();
            //st.Append("姓名：");
            //st.Append(par.STU_NAME);
            //st.Append("班级：");
            //st.Append(par.CLA_NAME);
            //st.Append("学院：");
            //st.Append(par.ACA_NAME);
            //stu_info.InnerText = st.ToString();
        }
        protected void ContestsList_SelectedIndexChanged(object sender, EventArgs e) 
        {
            BoParticipants par = participantService.getParticipant(ParId.Value);
            BoContests con = contestService.getContest(ContestsList.SelectedValue);
            BoCompetitions com = competitionService.getCompetition(con.CON_COMPETITIONS);
            if (com.COM_SPECIALTY == 0)
            {
                GroupsList.Enabled = false;
            }
            else {
                GroupsList.Enabled = true;
            }
            if (com.COM_TYPE == 0)
            {
                IsSpecialtyList.Enabled = false;
            }
            else {
                IsSpecialtyList.Enabled = true;
            }
        }
        protected void GroupsList_SelectedIndexChanged(object sender, EventArgs e) { }

    }
}